import ResManager from "../manager/ResManager";

const { ccclass, property, requireComponent } = cc._decorator;

@ccclass()
@requireComponent(cc.Sprite)
export class RemoteSprite extends cc.Component {

    @property
    private imageUrl: string = "http://thirdwx.qlogo.cn/mmopen/vi_32/DYAIOgq83erD6MOUwRKV9NyBAqnoFDTnltzAe2zWOkKxyDOFibVBb1ZV5CaATJwYAuNqZ5sXMBC4c8iacaHDf8RA/132";

    private sprite: cc.Sprite = null;

    public onLoad() {
        this.sprite = this.node.getComponent(cc.Sprite);
        this.updateDisplay();
    }

    public setImageUrl(value: string) {
        this.imageUrl = value;
        this.updateDisplay();
    }

    private updateDisplay() {
        if (!this.imageUrl || this.imageUrl === "") {
            return;
        }
        this.onDestroy();
        ResManager.instance.setSpriteImage(this.sprite, this.imageUrl, true);
        
        
    }
    public onDestroy() {
        if (this.sprite.spriteFrame) {
            ResManager.instance.release(this.sprite.spriteFrame);
        }
    }
}
